﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for GameJoinWindow.xaml
    /// </summary>
    public partial class GameLobby : Window
    {
        public delegate void RequestJoinQuery(String message);
        public RequestJoinQuery requestJoinQuery;

        private String gameid;

        Lobby.RequestQuery requestLobbyTrigger;

        public GameLobby(Lobby.RequestQuery requestQuery, String gamename, String players, String maxPlayers, String lives, String time, String gameid)
        {
            requestLobbyTrigger = requestQuery;
            InitializeComponent();
            requestJoinQuery = new RequestJoinQuery(this.addJoinMessageDelegate);
            HTTP.registerGameQuery(requestJoinQuery);
            Closing += GameJoinWindow_Closing;
            int nOfPlayers = maxPlayers == "0" ? 10 : maxPlayers == "1" ? 20 : maxPlayers == "2" ? 30 : 30;
            GameNameJoinLabel.Content = gamename;
            GameSizeJoinLabel.Content = "Type: " + nOfPlayers / 2 + "v" + nOfPlayers / 2;
            GameLivesJoinLabel.Content = "Lives: " + lives;
            GameTimeJoinLabel.Content = " Time: " + time;
            String[] playerArr = players.Split('-');
            foreach (String s in playerArr)
            {
                GameJoinPlayerListBox.Items.Add(s);
            }
            this.gameid = gameid;
        }

        /*public GameLobby()
        {
            requestJoinQuery = new RequestJoinQuery(this.addJoinMessageDelegate);
            HTTP.registerGameQuery(requestJoinQuery);
            InitializeComponent();
            Closing += GameJoinWindow_Closing;
        }*/


        void GameJoinWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            HTTP.sendToServer("type=ingame&subtype=remove-player&gameid=" + gameid + "&player=" + Lobby.getUsername());
            requestLobbyTrigger.Invoke("enableCreateGame");
        }

        public void addJoinMessageDelegate(String message)
        {
            String[] msg = message.Split('&');
            string type = "";
            String messageData = "";
            int count = 0;
            foreach (String m in msg)
            {
                if (type == "")
                {
                    if (m == "type=add")
                    {
                        type = "add";
                    }
                    else if (m == "type=remove")
                    {
                        type = "remove";
                    }
                    else if (m == "type=close")
                    {
                        type = "close";
                    }
                    else if (m == "type=startgame")
                    {
                        GameNameJoinLabel.Content = "Starts in: 5";
                        type = "startgame";
                    }
                    else if (m == "type=settings")
                    {
                        type = "settings";
                    }
                    
                }
                if (count == msg.Length-1)
                {
                    messageData = m.Split('=')[1];
                    if (type == "add")
                    {
                        GameJoinPlayerListBox.Dispatcher.BeginInvoke(new Action(delegate()
                        {
                            GameJoinPlayerListBox.Items.Add(messageData);
                        }));
                    }
                    if (type == "remove")
                    {
                        GameJoinPlayerListBox.Dispatcher.BeginInvoke(new Action(delegate()
                        {
                            GameJoinPlayerListBox.Items.RemoveAt(GameJoinPlayerListBox.Items.IndexOf(messageData));
                        }));
                    }
                    if (type == "close")
                    {
                        MessageBox.Show("Game has closed. Window will automatically close.");
                        this.Dispatcher.BeginInvoke(new Action(delegate()
                            {
                                requestLobbyTrigger.Invoke("enableCreateGame");
                                this.Close();
                            }));
                    }
                    if (type == "startgame")
                    {
                        Thread.Sleep(5000);
                        MessageBox.Show("GAME HAS STARTED! =D");
                        //start game here TODO

                    }

                    if (type == "settings")
                    {
                        String[] settingsdata = messageData.Split('-');
                        int players = Convert.ToInt32(HTTP.decipherGameType(settingsdata[0])) / 2;
                        GameSizeJoinLabel.Dispatcher.BeginInvoke(new Action(delegate()
                        {
                            GameSizeJoinLabel.Content = "Type: " + players + "v" + players;
                        }));
                        GameLivesJoinLabel.Dispatcher.BeginInvoke(new Action(delegate()
                        {
                            GameLivesJoinLabel.Content = "Lives: " + settingsdata[1];
                        }));
                        GameTimeJoinLabel.Dispatcher.BeginInvoke(new Action(delegate()
                        {
                            GameTimeJoinLabel.Content = " Time: " + settingsdata[2];
                        }));
                        
                        
                    }
                }
                count++;
            }
        }
    }
}
